package com.zhongxiangspace.controller.pagemanage;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zhongxiangspace.result.*;
import com.zhongxiangspace.entity.County;
import com.zhongxiangspace.entity.QueryAnalyzer;
import com.zhongxiangspace.service.CountyService;
import com.zhongxiangspace.tools.DateUtil;
import com.zhongxiangspace.tools.FileUtil;
import com.zhongxiangspace.tools.ReadProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
public class TradeAreaController {
    @Autowired
    private CountyService countyService;
    @Autowired
    private ReadProperties readProperties;

    /**
     * 添加商圈
     * @return
     */
    @RequestMapping("/basics/tradeArea/add.do")
    public String add(HttpServletRequest request, Model model) {
        Integer parent_id=0;
       List<County> county=this.countyService.queryAllType(parent_id);
       model.addAttribute("data",county);
        return "basics/tradeArea/trade_area_add";
    }

    /**
     *添加
     * @return
     */
    @ResponseBody
    @PostMapping("/basics/tradeArea/insert.do")
    public Result insert(County county) {
        county.setModify_time(DateUtil.getDateTime());
        county.setCreate_time(DateUtil.getDateTime());
        boolean flag=this.countyService.insert(county);
        return new NormalResult("success");
    }
    /**
     * 商圈列表
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/basics/tradeArea/list.do")
    public String list(HttpServletRequest request, Model model) {
        Integer parent_id=0;
        List<County> county=this.countyService.queryAllType(parent_id);
        model.addAttribute("data",county);
        return "basics/tradeArea/trade_area_list";
    }

    /**
     * 异步获取 data
     * @param queryAnalyzer
     * @return
     */
    @ResponseBody
    @GetMapping("/basics/tradeArea/ajaxList.do")
    public Response ajaxList(QueryAnalyzer queryAnalyzer) {
        PageHelper.startPage(queryAnalyzer.getPage(),queryAnalyzer.getLimit(),"parent_id DESC ");
        Page<Map<String,Object>>  list_banner=(Page<Map<String,Object>>) this.countyService.queryTradeAreaByKeyword(queryAnalyzer);
        Response result= new Response(0,"",list_banner.getTotal(),list_banner.getResult());
        return result;
    }


    /**
     * 异步删除 data
     * @return
     */
    @ResponseBody
    @PostMapping("/basics/tradeArea/delete.do")
    public Response delete(Integer id) {
        if(id!=null){
            boolean flag=this.countyService.delete(id);
        }
        Response result= new Response(0,"success",0,null);
        return result;
    }


    /**
     * 异步热门和非热门
     * @return
     */
    @ResponseBody
    @PostMapping("/basics/tradeArea/status-hot.do")
    public Response statusHot(Integer id) {
        int i=1;
        if(id!=null){
            County county=this.countyService.queryOne(id);
            if(county!=null&& county.getHotCode().equals(0)){
                county.setHotCode(1);
                boolean flag=this.countyService.updataHotCode(county);
            }else if(county!=null&& county.getHotCode().equals(1)){
                i=0;
                county.setHotCode(0);
                boolean flag=this.countyService.updataHotCode(county);
            }
        }
        Response result= new Response(0,"success",0,null);
        return result;
    }

    /**
     * 异步修改上下线 data
     * @return
     */
    @ResponseBody
    @PostMapping("/basics/tradeArea/status.do")
    public Response status(Integer id) {
        if(id!=null){
            County county=this.countyService.queryOne(id);
            if(county!=null&& county.getShowCode().equals(0)){
                int i=1;
                county.setShowCode(1);
                boolean flag=this.countyService.updataStatusCode(county);
            }else if(county!=null&& county.getShowCode().equals(1)){
                county.setShowCode(0);
                boolean flag=this.countyService.updataStatusCode(county);
            }
        }
        Response result= new Response(0,"success",0,null);
        return result;
    }

    /**
     * bnner 更改
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/basics/tradeArea/edit.do")
    public String edit(HttpServletRequest request, Model model,Integer id) {
        Integer parent_id=0;
        List<County> county_list=this.countyService.queryAllType(parent_id);
        model.addAttribute("data",county_list);
        County county=this.countyService.queryOne(id);
        model.addAttribute("object",county);
        return "basics/tradeArea/trade_area_updata";
    }
    /**
     *修改 updata
     * @return
     */
    @ResponseBody
    @PostMapping("/basics/tradeArea/updata.do")
    public Result updata(County c) {
        System.out.println(c.toString());
        County county=this.countyService.queryOne(c.getCounty_id());
        if(county!=null){
            int i=1;
            county.setParent_id(c.getParent_id());
            county.setModify_time(DateUtil.getDateTime());
            county.setImgPath(c.getImgPath());
            county.setDescription(c.getDescription());
            county.setName(c.getName());
            county.setCoordinate(c.getCoordinate());
            boolean flag=this.countyService.updata(county);
        }
        return new NormalResult("success");
    }

    /**
     * 上传banner图片
     *
     * @param multipartFile
     * @return
     */
    @ResponseBody
    @PostMapping("/basics/tradeArea/upload.do")
    public Result upload(@RequestParam("file") MultipartFile multipartFile) {
        Map<String, Object> map = null;
        try {
            if (!multipartFile.isEmpty()) {
                map = FileUtil.saveFile(multipartFile, "tradeArea", readProperties.getImagePath());
            }
        } catch (Exception e) {
            return new ExceptionResult(e);
        }
        return new NormalResult(map);
    }
}
